const hgService = uni.requireNativePlugin("HG-Background");

export default {
	init: () => {
		if (hgService) {

			hgService.config({
				title: "服务名称",
				content: "前台服务运行中",
				mode: 0, //0省电模式 1流氓模式
			});

			hgService.showSafeSetting(); //支持小米，华为，锤子，opp,vivo,三星，乐视，魅族
			hgService.requestIgnoreLimit();
		}
		// #ifdef APP-PLUS
		plus.push.setAutoNotification(true); //设置通知栏显示通知 //必须设置
		plus.push.addEventListener("click", function(msg) {
			console.log(msg, 11111111111);
			plus.push.clear(); //清空通知栏
			pushHandle(msg) //处理方法
		}, false);
		// 监听在线消息事件    
		plus.push.addEventListener("receive", function(msg) {
			console.log("receive:" + JSON.stringify(msg));
			if (plus.os.name == 'iOS') { //由于IOS 必须要创建本地消息 所以做这个判断
				if (msg.payload && msg.payload != null && msg.type == 'receive') {
					console.log(msg);
					// {"title": "xxx","content": "xxx","payload": "xxx"} 符合这种 才会自动创建消息  文档地址https://ask.dcloud.net.cn/article/35622
					plus.push.createMessage(msg.title, msg.content, JSON.stringify(msg.payload)) //创建本地消息
				}
			}
			if (plus.os.name == 'Android') {
				let options = {
					cover: false,
					sound: "system",
					title: msg.title
				}
				plus.push.createMessage(msg.content, msg.payload.content, options);
				// if(!msg.title||!msg.content||!msg.payload){ //  不符合自动创建消息的情况
				// 	 //这里根据你消息字段来创建消息 
				// 	 console.log("这里根据你消息字段来创建消息："+msg.title+","+msg.content+","+msg.payload);
				// 	plus.push.createMessage(msg.payload.content,JSON.stringify(msg.payload))  //创建本地消息
				// }else{
				// 	//符合自动创建消息 
				// 	console.log("符合自动创建消息"+msg.title+","+msg.content+","+msg.payload);
				// 	pushHandle(msg)
				// }	
			}

		}, false);
		// #endif
	},

	getClient: (callback) => {
		// #ifdef APP-PLUS
		let clientInfo = plus.push.getClientInfo(); //获取 clientID
		uni.setStorageSync('clientid', clientInfo.clientid)
		console.log(clientInfo, 5555555);
		const userName = uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo').data.personId : ''
		bindAliasAndTag(userName)
		// #endif

	},


}
//绑定别名
const bindAliasAndTag = (alias, tags) => {
	var isAndorid, PushManager, context, Instance, GeTuiSdk;

	if (plus.os.name == 'Android') {
		isAndorid = true;
	} else {
		isAndorid = false;
	}

	if (isAndorid) {
		PushManager = plus.android.importClass("com.igexin.sdk.PushManager");
		console.log(PushManager, 111111);
		context = plus.android.runtimeMainActivity().getContext();
		console.log(context, 2222222);
		Instance = PushManager.getInstance();
		console.log(Instance, 33333333333)
	} else {
		GeTuiSdk = plus.ios.importClass("GeTuiSdk");
	}

	if (isAndorid) {
		console.log('获取到cid==', Instance.getClientid(context));
		console.log('获取到版本号==', Instance.getVersion(context));
		console.log('获取到tags==', JSON.stringify(tags));
		console.log('获取到alias==', alias);

		//绑定别名
		Instance.bindAlias(context, alias);

		//绑定标签
		var Tag = plus.android.importClass("com.igexin.sdk.Tag");
		var tagParam = new Array();
		for (let i = 0; i < tags.length; i++) {
			var tag = new Tag();
			tag.setName(tags[i]);
			tagParam[i] = tag;

			console.log('tags[' + i + ']==', tags[i]);
		}
		console.log('tagParam==', JSON.stringify(tagParam));
		Instance.setTag(context, tagParam, Date.now() + "");
	} else {
		GeTuiSdk.bindAliasandSequenceNum(alias, alias);
		GeTuiSdk.setTags(alias);
	}
}
const pushHandle = (msg) => {
	console.log(msg, 999999999);
	if (typeof(msg.payload) == 'string') { //如果是字符串，表示是ios创建的  要转换一下
		msg.payload = JSON.parse(msg.payload)
	}
	if (!msg) return false;
	plus.runtime.setBadgeNumber(0); //清除app角标

	//下面的代码根据自己业务来写 这里可以写跳转业务代码
	//跳转到tab
	// if (msg.payload.pathType == '1') {
	// 	uni.switchTab({
	// 		url: msg.payload.url
	// 	})
	// }
	//跳转到详情
	// if (msg.payload.pathType == 0) {
	// 	let url = msg.payload.url
	// 	if (msg.payload.args) {
	// 		url = url + '?id=' + msg.payload.args
	// 	}
	// 	console.log(url);
	// 	uni.navigateTo({
	// 		url: url
	// 	})
	// }
}